<template>
    <el-row justify="center" align="middle" style="height: 100vh;">
        <el-col :span="16" :offset="4">
            <el-card>
                <h2 style="text-align:center;">登录</h2>
                <el-form :model="loginForm" :rules="rules" ref="loginRef" label-width="60px">
                    <el-form-item label="账号" prop="username">
                        <el-input v-model="loginForm.username" placeholder="请输入账号" />
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" />
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onLogin">登录</el-button>
                    </el-form-item>
                </el-form>
            </el-card>
        </el-col>
    </el-row>
</template>

<script>
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

export default {
    name: 'Login',
    data() {
        return {
            loginForm: {
                username: '',
                password: ''
            },
            rules: {
                username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
            }
        }
    },
    methods: {
        onLogin() {
            this.$refs.loginRef.validate((valid) => {
                if (valid) {
                    if (this.loginForm.username === 'admin' && this.loginForm.password === '123456') {
                        this.$router.push('/home')
                    } else {
                        console.log('登录失败')
                        ElMessage.error('账号或密码错误')
                    }
                }
            })
        }
    }
}
</script>
